﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.audio {
	import flash.events.EventDispatcher;
	import nl.sandmen.team.data.Album;
	import nl.sandmen.team.events.AlbumPlayerEvent;
	import nl.sandmen.team.events.TrackPlayerEvent;
	import nl.sandmen.team.main.TitleController;
	/**
	 * @author Team Sandmen
	 */
	public class AlbumPlayer extends EventDispatcher {
		private var _currentTrack:uint;
		private var currentArtist:String;
		private var currentTrackList:Array;
		private var currentTrackPlayer:TrackPlayer;
		private var nextTrackPlayer:TrackPlayer;
		public function AlbumPlayer():void {
			currentTrackPlayer = new TrackPlayer();
			currentTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_PRELOADED, preloaded);
			currentTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_PROGRESS, progress);
			currentTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_END, end);
			nextTrackPlayer = new TrackPlayer();
			nextTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_PRELOADED, preloaded);
			nextTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_PROGRESS, progress);
			nextTrackPlayer.addEventListener(TrackPlayerEvent.TRACK_PLAYER_END, end);
		}
		public function get currentTrack():uint {
			return _currentTrack;
		}
		public function set currentTrack(value:uint):void {
			_currentTrack = value;
			if (currentTrackList.length <= value) {
				dispatchEvent(new AlbumPlayerEvent(AlbumPlayerEvent.NEXT_ALBUM));
			} else {
				if (currentTrackList[value] !== currentTrackPlayer.track) {
					currentTrackPlayer.stop();
					if (currentTrackList[value] !== nextTrackPlayer.track) {
						currentTrackPlayer.preload(currentTrackList[value]);
					} else {
						// If the track that should be played is the track that is preloaded/preloading in nextTrackPlayer:
						var nextTrackPlayer:TrackPlayer = currentTrackPlayer;
						currentTrackPlayer = this.nextTrackPlayer;
						this.nextTrackPlayer = nextTrackPlayer;
						if (currentTrackPlayer.preloaded) {
							nextTrackPlayer.preload(currentTrackList[value + 1]);
						} else {
							currentTrackPlayer.preload(currentTrackList[value]);
						}
					}
				}
				currentTrackPlayer.play();
				TitleController.setTitle(currentArtist + " - " + currentTrackList[value].name);
			}
		}
		private function end(e:TrackPlayerEvent):void {
			trackDown();
		}
		public function pause():void {
			currentTrackPlayer.stop();
		}
		public function play():void {
			currentTrackPlayer.resume();
		}
		public function playAlbum(album:Album):void {
			currentTrackPlayer.stop();
			currentTrackList = album.trackList;
			currentArtist = album.artist;
			nextTrackPlayer.stopPreloading();
			currentTrack = 0;
		}
		private function preloaded(e:TrackPlayerEvent):void {
			// If the currentTrackPlayer has finished loading the song, we can preload the nextTrackPlayer in advance.
			if (e.currentTarget === currentTrackPlayer) {
				nextTrackPlayer.preload(currentTrackList[_currentTrack + 1]);
			}
		}
		private function progress(e:TrackPlayerEvent):void {
			if (e.currentTarget === currentTrackPlayer) {
				trace(uint(e.progress * 100));
			}
		}
		public function trackDown():void {
			currentTrack = _currentTrack + 1;
		}
		public function trackUp():void {
			currentTrack = _currentTrack + 1;
		}
	}
}